<?php
namespace app\common\taglib;
use think\template\TagLib;
use think\Db;

class MyTag extends TagLib{
    /**
     * 定义标签列表
     */
    protected $tags   =  [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'game'     => ['attr' => 'type,id,disabled', 'close' => 0], //闭合标签，默认为不闭合
        'channel'     => ['attr' => 'type,id,disabled', 'close' => 0], //闭合标签，默认为不闭合
        'umditor'     => ['attr' => 'name,content,height', 'close' => 0], //闭合标签，默认为不闭合
        'client' => ['attr' => 'name', 'close' => 0],
    ];

    public function tagGame($tag)
    {
        $where = [];

        $type = empty($tag['type']) ? 'manage' : $tag['type'];
        $id = empty($tag['id']) ? '' : $tag['id'];
        $disabled = empty($tag['disabled']) ? '' : 'disabled';

        switch ($type){
            case 'manage':
                $where['status'] = ['<>', -1];
                $game = Db::name('ViewGameManage')->field('game_id, game_name')->where($where)->order('game_id desc')->select();
                break;
            case 'channel':
                $where['status'] = 1;
                $game = Db::name('ViewGameManage')->field('game_id, game_name')->where($where)->order('game_id desc')->select();
                break;
        }

        $parse = '<select name="game_id" id="game_id" lay-search="" lay-filter="game_id" '.$disabled.'>';
        $parse .= '<option value="">选择游戏（全部）</option>';
        foreach ($game as $k=>$v) {
            $selected = $id ? '<?php echo '.$v['game_id'].'!='.$id.' ? "" : "selected";?> ' : '';
            $parse .= '<option '.$selected." value='{$v['game_id']}'>{$v['game_id']}：{$v['game_name']}</option>";
        }
        $parse .= '</select>';

        return $parse;
    }

    public function tagChannel($tag)
    {
        $where['status'] = 1;

        $type = empty($tag['type']) ? 'manage' : $tag['type'];
        $id = empty($tag['id']) ? '' : $tag['id'];
        $disabled = empty($tag['disabled']) ? '' : 'disabled';

        switch ($type){
            case 'manage':
                $select1 = Db::name('ViewChannel')->field('cps_id as channel_id, cps_name as channel_name')->where('channel_id', 1000)->order('cps_id desc')->select();
                $select2 = Db::name('ViewChannel')->field('channel_id, channel_name')->where('channel_id', '>=', 5000)->order('channel_id desc')->select();
                $channel = array_merge($select2, $select1);
                break;
            case 'channel':
                if($id<5000){
                    $allChannelId = model('manage/Channel')->allChannelId(session('admin.id'));
                    $where['cps_id'] = ['in', $allChannelId];
                    $channel = Db::name('ViewChannel')->field('cps_id as channel_id, cps_name as channel_name')->where($where)->order('cps_id desc')->select();
                }else{
                    $where['channel_id'] = session('admin.id');
                    $channel = Db::name('ViewChannel')->field('channel_id, channel_name')->where($where)->order('channel_id desc')->select();
                }
                break;
        }

        $parse = '<select name="channel_id" id="channel_id" lay-search="" lay-filter="channel_id" '.$disabled.'>';
        $parse .= '<option value="">选择渠道（全部）</option>';
        foreach ($channel as $k=>$v) {
            $selected = $id ? '<?php echo '.$v['channel_id'].'!='.$id.' ? "" : "selected";?> ' : '';
            $parse .= '<option '.$selected." value='{$v['channel_id']}'>{$v['channel_id']}：{$v['channel_name']}</option>";
        }
        $parse .= '</select>';

        return $parse;
    }

    public function tagUmditor($tag){
        $name = $tag['name'];
        $content = $tag['content'] ? '{'.$tag['content'].'|htmlspecialchars_decode}' : '';
        $height = $tag['height'];

        $php = <<<php
<script type="text/plain" id="myEditor" name="$name" style="width:100%;height:$height">$content</script>
<link href="__STATIC__/umeditor/themes/default/_css/umeditor.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="__STATIC__/umeditor/third-party/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/umeditor/umeditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__STATIC__/umeditor/editor_api.js"></script>
<script type="text/javascript" src="__STATIC__/umeditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
    var um = UM.getEditor('myEditor', {
        autoHeightEnabled: false
    });
</script>
php;
        return $php;
    }

    public function tagClient($tag){
        $name = $tag['name'];

        $appType = model('AppType')->select();
        $appPublishType = model('AppPublishType')->select();
        $appPlatformType = model('AppPlatformType')->select();
        $appClientType = model('AppClientType')->select();

        $html = '<input type="text" value="" placeholder="游戏终端" autocomplete="off" class="layui-input my-game-client">';
        $html .= '<div class="my-client" switch="off">';
        $html .= '<div class="my-client-li" id="game_server">';
        $html .= '<button class="my-client-head layui-btn my-btn" val="">';
        $html .= '部门（全部）';
        $html .= '</button>';
        foreach ($appType as $v){
            $html .= '<button class="layui-btn my-client-li-btn" val="'.$v['id'].'">'.$v['name'].'</button>';
        }
        $html .= '</div>';
        $html .= '<div class="my-client-li" id="game_publish">';
        $html .= '<button class="my-client-head layui-btn my-btn" val="">';
        $html .= '版本（全部）';
        $html .= '</button>';
        foreach ($appPublishType as $v){
            $html .= '<button class="layui-btn my-client-li-btn" val="'.$v['id'].'">'.$v['name'].'</button>';
        }
        $html .= '</div>';
        $html .= '<div class="my-client-li" id="game_platform">';
        $html .= '<button class="my-client-head layui-btn my-btn" val="">';
        $html .= '平台（全部）';
        $html .= '</button>';
        foreach ($appPlatformType as $v){
            $html .= '<button class="layui-btn my-client-li-btn" val="'.$v['id'].'">'.$v['name'].'</button>';
        }
        $html .= '</div>';
        $html .= '<div class="my-client-li" id="game_client">';
        $html .= '<button class="my-client-head layui-btn my-btn" val="">';
        $html .= '终端（全部）';
        $html .= '<buttonv>';
        foreach ($appClientType as $v){
            $html .= '<button class="layui-btn my-client-li-btn" val="'.$v['id'].'">'.$v['name'].'</button>';
        }
        $html .= '</div>';
        $html .= '</div>';
        $html .= "
            <script type='text/javascript' src='__STATIC__/js/bootselect/jquery-2.0.3.min.js'></script>
            <script>
            $('.my-client button').click(function(){
                $(this).addClass('my-btn').siblings().removeClass('my-btn');
                return false;
            });
            $('.my-game-client').click(function(){
                if( $('.my-client').attr('switch') == 'off' ){
                    $('.my-client').fadeIn(200);
                    $('.my-client').attr('switch','on');
                }else{
                    $('.my-client').fadeOut(200);
                    $('.my-client').attr('switch','off');
                }
            })
            $(document).bind('click',function(e){
                var target  = $(e.target);
                if(target.closest('.my-client,.my-game-client').length == 0){
                    $('.my-client').fadeOut(200);
                    $('.my-client').attr('switch','off');
                };
                e.stopPropagation();
            });
            </script>";
        return $html;
    }
}